package org.nokatag.tagjava;

import java.io.IOException;
import java.util.HashSet;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.nokatag.system.Concest;
import org.nokatag.system.ReValueDoc;
/**
 * html编辑器
 * @author rebin
 *
 */
public class HtmlEdit extends NokaTag{

	private static final long serialVersionUID = 7297781204324779417L;
	private String name=null;
	private String id = null;
	private String width="560";
	private String height="200";
	private String uid = null;//用户自定义过滤id
	private String filesize=null;//上传文件大小
	private String annextypedescription="images(*.jpg,*.gif,*.png,*.jpeg,*.bmp)";//附件显示类型
	private String annextypeextension="*.jpg;*.gif;*.png;*.jpeg;*.bmp;";//附件上传类型
	private String isdel = null;//是否可以删除
	private String type=null;
	private String value=null;
	private String insertHTML=null;//插入内容的方法名
	private String getValue=null;//获取值
	private String setValue=null;//设置值
	private Integer textsun=50;//备注字数
	private static final Integer textsunMax=100;
	
	public int doStartTag() throws JspException {
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		String basePath = basepath(request);
		JspWriter jspOut = pageContext.getOut();
		StringBuilder body = new StringBuilder();
		UUID uuid = UUID.randomUUID();
		String filetypedescription="images(*.jpg,*.gif,*.png,*.jpeg,*.bmp)";
		String filetypeextension="*.jpg;*.gif;*.png;*.jpeg;*.bmp;";
		if(this.getType()==null)
			  this.setType("pid");
		if(this.getIsdel()==null || this.getIsdel().equalsIgnoreCase("true"))
			  this.setIsdel("yes");
		  if(this.getIsdel()!=null && this.getIsdel().equalsIgnoreCase("false"))
			  this.setIsdel("no");
		if(this.getUid()==null)
			  this.setUid("99999999");
		if(this.getFilesize()==null)
			  this.setFilesize("500");
		//-------------------设置文件类型------------------
		 if(Concest.UPDOCTYPE==null){
			Concest.UPDOCTYPE=new HashSet<String>();
		 }
		 for(String doctyp:filetypeextension.split(";")){
			Concest.UPDOCTYPE.add(doctyp.substring(doctyp.lastIndexOf(".")+1,doctyp.length()));
		 }
		 for(String doctyp:annextypeextension.split(";")){
				Concest.UPDOCTYPE.add(doctyp.substring(doctyp.lastIndexOf(".")+1,doctyp.length()));
			 }
		  //-------------------------------------------------
		String sessionid = uid+","+pageContext.getSession().getId()+","+uuid.toString().replace("-", "_");
		String openurl=basePath+"/nokatag/file/upload/upload.html?filesize="+this.getFilesize()+"&filetypedescription="+filetypedescription+"&filetypeextension="+filetypeextension+"&sessionid="+sessionid+"&isdel="+this.getIsdel()+"&idreadio=true&type="+this.getType();
		String annexurl=basePath+"/nokatag/file/upload/upload.html?filesize="+this.getFilesize()+"&filetypedescription="+annextypedescription+"&filetypeextension="+annextypeextension+"&sessionid="+sessionid+"&isdel="+this.getIsdel()+"&idreadio=true&type="+this.getType();
		if(textsun>textsunMax){
			textsun=textsunMax;
		}
		openurl+="&tsun="+textsun;
		openurl+="&inputid=";
		annexurl+="&tsun="+textsun;
		annexurl+="&inputid=";
		
		Concest.PAGETEMP.put("insert_image.html_openurl",openurl);//注册
		Concest.PAGETEMP.put("insert_annex.html_openurl",annexurl);//注册
		Concest.REVALUELIST.add(new ReValueDoc("insert_image.html_id","wysiwyg","paramenter"));
		body.append(potjs(request));
		body.append(htmlEdit(request));
		if(value==null)
			value="";
		body.append("<div><textarea id=\""+id+"\" name=\""+name+"\" style=\"width:"+width+";height:"+height+";\">"+value+"</textarea>");
		body.append("<script type=\"text/javascript\">");
		body.append("Event.observe(document, 'dom:loaded', function() {");
		body.append("var nedit"+id+" = new nhtmledit('"+id+"');");
		body.append("nedit"+id+".show();");
		body.append("});");
		if(setValue!=null){
			body.append("function "+setValue+"(text){");
			body.append("	$('"+id+"').setValue(text); ");
			body.append("}");
		}
		if(getValue!=null){
			body.append("function "+getValue+"(){");
			body.append(" return $('"+id+"').getValue();");
			body.append("}");
		}
		if(insertHTML!=null){
			body.append("function "+insertHTML+"(htmlstr){");
			body.append("$('"+id+"').insertHTML(htmlstr);");
			body.append("}");
		}
		body.append("</script></div>");
		try {
			jspOut.write(body.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_BODY_INCLUDE;
	}

	public String getHeight() {
		return height;
	}

	public void setHeight(String height) {
		this.height = height;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getWidth() {
		return width;
	}

	public void setWidth(String width) {
		this.width = width;
	}

	public String getUid() {
		return uid;
	}

	public void setUid(String uid) {
		this.uid = uid;
	}

	public String getFilesize() {
		return filesize;
	}

	public void setFilesize(String filesize) {
		this.filesize = filesize;
	}

	public String getIsdel() {
		return isdel;
	}

	public void setIsdel(String isdel) {
		this.isdel = isdel;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getInsertHTML() {
		return insertHTML;
	}

	public void setInsertHTML(String insertHTML) {
		this.insertHTML = insertHTML;
	}

	public String getGetValue() {
		return getValue;
	}

	public void setGetValue(String getValue) {
		this.getValue = getValue;
	}

	public String getSetValue() {
		return setValue;
	}

	public void setSetValue(String setValue) {
		this.setValue = setValue;
	}

	public Integer getTextsun() {
		return textsun;
	}

	public void setTextsun(Integer textsun) {
		this.textsun = textsun;
	}

	public String getAnnextypedescription() {
		return annextypedescription;
	}

	public void setAnnextypedescription(String annextypedescription) {
		this.annextypedescription = annextypedescription;
	}

	public String getAnnextypeextension() {
		return annextypeextension;
	}

	public void setAnnextypeextension(String annextypeextension) {
		this.annextypeextension = annextypeextension;
	}

}
